package com.kzd.gtgame.jetpack.manager

import com.kzd.gtgame.app.*
import com.kzd.gtgame.base.BaseManager
import com.kzd.gtgame.entity.*
import com.module.lemlin.http.CustomException
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.HttpResponseDataT
import rxhttp.RxHttp
import rxhttp.await

class AccessRepository : BaseManager() {

    /**
     * 获取验证码
     * */
    suspend fun accessGetCode(phone: String, type: Int): HttpResponseDataT<String> =
        RxHttp.postForm(USER_MOBILE_CODE)
            .add("phone", phone)
            .add("type", type)
            .await()

    /**
     * 用户名注册
     * */
    suspend fun accessRegName1(
        userName: String, passwd: String, type: String, invite: String? = null,
        code: String? = null, sessionid: String? = null,
    ): HttpResponseDataT<AccessUser> {
        if (SPSConstance.isFirstInstall == false) {
            RxHttp.postForm(APP_INSTALL_FINISH)
                .add("device_id", SPSConstance.deviceId ?: "")
                .await<HttpResponseData>()
        }
        val http = RxHttp.postForm(USER_USERNAME_REG)
            .add("user_name", userName)
            .add("passwd", passwd)
            .add("type", type)
        if (!invite.isNullOrEmpty()) {
            http.add("invite", invite)
        }
        if (!code.isNullOrEmpty()) {
            http.add("code", code)
        }
        if (!sessionid.isNullOrEmpty()) {
            http.add("sessionid", sessionid)
        }
        val deviceId = SPSConstance.deviceId
        if (!deviceId.isNullOrEmpty()) {
            http.add("device_id", deviceId)
        }
        val uidResponse = http.await<HttpResponseDataT<String>>()
        if (uidResponse.code != 1) {
            throw CustomException(uidResponse.code.toString(), uidResponse.message)
        }
        val userInfoResponse = RxHttp.postForm(USER_GET_USER_INFO)
            .setAssemblyEnabled(false)
            .add("uid", uidResponse.data)
            .await<HttpResponseDataT<UserInfo>>()
        val userMineResponse = RxHttp.postForm(USER_MY_INFO)
            .setAssemblyEnabled(false)
            .add("uid", uidResponse.data)
            .await<HttpResponseDataT<UserMine>>()
        val accessUser = AccessUser(
            uid = uidResponse.data,
            userInfo = userInfoResponse.data,
            userMine = userMineResponse.data
        )
        return HttpResponseDataT(accessUser).apply {
            this.code = uidResponse.code
            message = uidResponse.message
        }
    }

    /**
     * 用户名登录
     * */
    suspend fun accessLoginName1(
        userName: String, passwd: String, time: String, sign: String,
    ): HttpResponseDataT<AccessUser> {

        val uidResponse = RxHttp.postForm(APP_LOGIN)
            .add("user_name", userName)
            .add("passwd", passwd)
            .add("sign", sign)
            .add("t", time)
            .await<HttpResponseDataT<String>>()
        if (uidResponse.code != 1) {
            throw CustomException(uidResponse.code.toString(), uidResponse.message)
        }
        val userInfoResponse = RxHttp.postForm(USER_GET_USER_INFO)
            .setAssemblyEnabled(false)
            .add("uid", uidResponse.data)
            .await<HttpResponseDataT<UserInfo>>()
        val userMineResponse = RxHttp.postForm(USER_MY_INFO)
            .setAssemblyEnabled(false)
            .add("uid", uidResponse.data)
            .await<HttpResponseDataT<UserMine>>()
        val accessUser = AccessUser(
            uid = uidResponse.data,
            userInfo = userInfoResponse.data,
            userMine = userMineResponse.data
        )
        return HttpResponseDataT(accessUser).apply {
            code = uidResponse.code
            message = uidResponse.message
        }
    }

    /**
     * 用户一键登录
     * */
    suspend fun accessLoginOneKey(
        token: String, time: String, sign: String,
    ): HttpResponseDataT<AccessUser> {
        val uidResponse = RxHttp.postForm(APP_LOGIN_ONE_KEY)
            .add("token", token)
            .add("sign", sign)
            .add("t", time)
            .await<HttpResponseDataT<String>>()
        if (uidResponse.code != 1) {
            throw CustomException(uidResponse.code.toString(), uidResponse.message)
        }
        val userInfoResponse = RxHttp.postForm(USER_GET_USER_INFO)
            .setAssemblyEnabled(false)
            .add("uid", uidResponse.data)
            .await<HttpResponseDataT<UserInfo>>()
        val userMineResponse = RxHttp.postForm(USER_MY_INFO)
            .setAssemblyEnabled(false)
            .add("uid", uidResponse.data)
            .await<HttpResponseDataT<UserMine>>()
        val accessUser = AccessUser(
            uid = uidResponse.data,
            userInfo = userInfoResponse.data,
            userMine = userMineResponse.data
        )
        return HttpResponseDataT(accessUser).apply {
            code = uidResponse.code
            message = uidResponse.message
        }
    }

    /**
     * 修改密码(需要登录操作)
     * */
    suspend fun accessChangePwd(
        sign: String, time: String, oldpasswd: String, passwd: String, repasswd: String,
    ): HttpResponseData = RxHttp.postForm(APP_EDIT_PASSWD)
        .add("sign", sign)
        .add("t", time)
        .add("oldpasswd", oldpasswd)
        .add("passwd", passwd)
        .add("repasswd", repasswd)
        .await()

    /**
     * 设置密码
     * */
    suspend fun accessSettingPwd(
         passwd: String, repasswd: String,
    ): HttpResponseData = RxHttp.postForm(APP_SETTING_PASSWD)
        .add("passwd", passwd)
        .add("repasswd", repasswd)
        .await()

    /**
     * 绑定手机号(需要登录操作)
     * */
    suspend fun accessBindPhone(
        sign: String, time: String, phone: String, sessionid: String, code: String,
    ): HttpResponseDataT<UserInfo> = RxHttp.postForm(APP_BIND_PHONE)
        .add("sign", sign)
        .add("t", time)
        .add("phone", phone)
        .add("sessionid", sessionid)
        .add("code", code)
        .await()

    /**
     * 换绑手机号验证(需要登录操作)
     * */
    suspend fun accessVerifyPhone(
        phone: String, code: String, sessionid: String,
    ): HttpResponseData = RxHttp.postForm(APP_VERIFY_CODE)
        .add("phone", phone)
        .add("code", code)
        .add("sessionid", sessionid)
        .await()

    /**
     * 忘记密码
     * */
    suspend fun accessForgetPwd(
        phone: String, passwd: String, confirm_passwd: String, code: String, sessionid: String,
    ): HttpResponseData = RxHttp.postForm(USER_FORGET_PASSWORD)
        .add("phone", phone)
        .add("passwd", passwd)
        .add("confirm_passwd", confirm_passwd)
        .add("code", code)
        .add("sessionid", sessionid)
        .await()

}